<?php

/**
 * Created by PhpStorm.
 * User: jingshou
 * Date: 2017/9/5
 * Time: 21:35
 */

namespace app\admin\controller;

use app\admin\model\Type;
use think\Controller;
use app\admin\model\Cases as CaseModel;
use think\Request;

class Cases extends Controller
{
    public function type_list()
    {
        $data = Request::instance()->get();
        $data = array_merge($data, ['type' => CaseModel::TYPE]);
        $model = Type::typeList($data);
        return $this->fetch('type_list', [
            'model' => $model,
        ]);
    }

    public function type_add()
    {
        $model = Type::get(0);
        return $this->fetch('type_add', [
            'model' => $model,
        ]);
    }

    public function type_detail($id)
    {
        $model = Type::get($id);
        return $this->fetch('type_add', [
            'model' => $model,
        ]);
    }

    public function type_del($id)
    {
        return Type::typeDel($id);
    }

    public function addAndUpdate()
    {
        $data = Request::instance()->post();
        if (empty($data)) {
            return resError('参数错误');
        }

        if (empty($data['id'])) {
            return CaseModel::typeAdd($data);
        } else {
            return CaseModel::typeUpdate($data);
        }
    }

    public function news_list()
    {
        $data = Request::instance()->get();
        $list = CaseModel::newsList($data);

        $typeList = Type::typeList(['type' => CaseModel::TYPE]);
        return $this->fetch('list', [
            'list' => $list,
            'typeList' => $typeList,
        ]);
    }

    public function news_add()
    {
        $model = CaseModel::get(0);

        $list = Type::typeList(['type' => CaseModel::TYPE, 'state' => 0]);
        return $this->fetch('add', [
            'model' => $model,
            'list' => $list,
        ]);
    }

    public function detail($id)
    {
        $model = CaseModel::get($id);

        $list = Type::typeList(['type' => CaseModel::TYPE, 'state' => 0]);
        return $this->fetch('add', [
            'model' => $model,
            'list' => $list,
        ]);
    }

    public function newsDel($id)
    {
        return CaseModel::newsDel($id);
    }

    public function addAndUpdateNews()
    {
        if (Request::instance()->isPost()) {
            $data = Request::instance()->post();
            if (!empty($data['id'])) {
                return CaseModel::newsUpdate($data);
            } else {
                return CaseModel::add($data);
            }
        } else {
            return resError('提交类型错误');
        }
    }
}